package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jaiky.imagespickers.Image;
import com.jaiky.imagespickers.ImageConfig;
import com.jaiky.imagespickers.ImageSelector;
import com.jaiky.imagespickers.ImageSelectorActivity;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Status;
import com.neusoft.zqygbc.neusoft_wenda.model.UserModel;
import com.neusoft.zqygbc.neusoft_wenda.service.UserService;
import com.neusoft.zqygbc.neusoft_wenda.utils.GlideLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends BaseAvtivity {
    private LinearLayout reg_return;
    private EditText username;
    private EditText password;
    private EditText passwordagain;
    private Button btn_reg;
    private UserModel userModel;
    private Call call;
    private RetrofitCallback<Status> statusRetrofitCallback;
    private Intent intent;
    private String utel;

    @Override
    void initViews() {
        setLayout(R.layout.activity_register);
        intent = getIntent();
        utel = intent.getStringExtra("utel");
        username = (EditText) findViewById(R.id.reg_username);
        password = (EditText) findViewById(R.id.reg_password);
        passwordagain = (EditText) findViewById(R.id.reg_passwordagain);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        reg_return = (LinearLayout) findViewById(R.id.reg_return);
    }

    @Override
    void initEvents() {
//        select_upic.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                selectphoto();
//            }
//        });
        reg_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent1);
            }
        });


        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //拿到用户名、密码、确认密码EditText的用户输入
                String str_user = username.getText().toString();
                String str_psd = password.getText().toString();
                String str_psdagain = passwordagain.getText().toString();
                if (str_user.equals("") || str_psd.equals("")) {//输入有为空的项
                    Toast.makeText(RegisterActivity.this, "亲还有项没有写~", Toast.LENGTH_SHORT).show();
                } else {
                    if (str_psd.equals(str_psdagain)) {
                        register();

                    } else {
                        Toast.makeText(RegisterActivity.this, "两次密码输入不相同哦~", Toast.LENGTH_SHORT).show();
                    }
                }
            }

        });

    }

    @Override
    void initData() {
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
//
//            tv1.setText("");
//            for (String path : pathList) {
//                tv1.append(path);
//                tv1.append("\n");
//            }
//
//            path.clear();
//            path.addAll(pathList);
//        }
//    }

//    private View.OnClickListener btnlistener = new View.OnClickListener() {
//
//        @Override
//        public void onClick(View v) {
//            switch (v.getId()) {
//                case R.id.reg_btn_open_camera: // 打开照相机
//
//                    Toast.makeText(RegisterActivity.this, "照相机", Toast.LENGTH_SHORT).show();
//                    imageConfig = new ImageConfig.Builder(
//                            new GlideLoader())
//                            .steepToolBarColor(getResources().getColor(R.color.titleBlue))
//                            .titleBgColor(getResources().getColor(R.color.titleBlue))
//                            .titleSubmitTextColor(getResources().getColor(R.color.white))
//                            .titleTextColor(getResources().getColor(R.color.white))
//                            // 开启单选   （默认为多选）
//                            .singleSelect()
//                            // 裁剪 (只有单选可裁剪)
//                            .crop()
//                            // 开启拍照功能 （默认关闭）
//                            .showCamera()
//                            //设置显示容器
//                            .setContainer(llContainer)
//                            .requestCode(REQUEST_CODE)
//                            .build();
//                    ImageSelector.open(RegisterActivity.this, imageConfig);
//
//
//                    break;
//                // 取消
//                case R.id.reg_btn_cancel:
//                    if (mCameraDialog != null) {
//                        mCameraDialog.dismiss();
//                    }
//                    break;
//            }
//        }
//    };
//
//    private void selectphoto() {
//
//
//        mCameraDialog = new Dialog(this, R.style.my_dialog);
//        LinearLayout root = (LinearLayout) LayoutInflater.from(this).inflate(
//                R.layout.camera_control, null);
//        root.findViewById(R.id.reg_btn_open_camera).setOnClickListener(btnlistener);
//        root.findViewById(R.id.reg_btn_cancel).setOnClickListener(btnlistener);
//        mCameraDialog.setContentView(root);
//        Window dialogWindow = mCameraDialog.getWindow();
//        dialogWindow.setGravity(Gravity.BOTTOM);
//        dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//        lp.x = 0; // 新位置X坐标
//        lp.y = 100; // 新位置Y坐标
//        lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
//        root.measure(0, 0);
//        lp.height = root.getMeasuredHeight();
//        lp.alpha = 9f; // 透明度
//        dialogWindow.setAttributes(lp);
//        mCameraDialog.show();
//
//    }


    protected void register() {
        userModel = new UserModel();
        call = userModel.register(username.getText().toString(),
                password.getText().toString(),
                utel);
        statusRetrofitCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("utel", utel);//放参数
                    setResult(RESULT_OK, intent);//回到父acttibity
                    finish();
                } else {

                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                }

            }
        };
        userModel.bindCallback(call, statusRetrofitCallback);
    }


}
